package view.controller;

import common.Group;
import common.Message;
import common.NowUser;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import server.ClientConnServer;

import javax.swing.*;
import java.io.IOException;
import java.util.concurrent.BrokenBarrierException;

public class DissolutiongroupController {
    public Button dissolutiontrue;
    public Button dissolutionfalse;
    public Label groupid;
    public ImageView dissolutionclose;

    public void todissolutionclose(MouseEvent mouseEvent) {
        Stage stage = (Stage) dissolutionclose.getScene().getWindow();
        stage.close();
    }

    public void ondissolutionfalse(ActionEvent actionEvent) {
        Stage stage = (Stage) dissolutionfalse.getScene().getWindow();
        stage.close();
    }

    public void ondissolutiontrue(ActionEvent actionEvent) throws BrokenBarrierException, InterruptedException, IOException {
        Group group = new Group();
        //Common common = new Common();
        Message msg = new Message();
        group.setuserid(NowUser.getUser().getId());
        group.setGroupid(NowUser.getGroup().getGroupid());
        //group.setGroupid(dissolutiongroupid.getText());
        System.out.println("即将被解散群聊的id:" + NowUser.getGroup().getGroupid());
        msg.setGroup(group);
        System.out.println(msg.getGroup().getGroupid());
        msg.setType("dissolutiongroup");
        NowUser.dissolutiongroup=false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        if(NowUser.dissolutiongroup) {
            System.out.println("解散群聊");
            JOptionPane.showMessageDialog(null, "群聊解散成功", "提示", JOptionPane.PLAIN_MESSAGE);
            Stage stage = (Stage) groupid.getScene().getWindow();
            stage.close();
        } else{
            JOptionPane.showMessageDialog(null, "群聊解散失败", "提示", JOptionPane.ERROR_MESSAGE);
        }
    }
}
